package com.mjh.zhujianju.controller.base;

import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.utils.XTCurrentUser;
import com.mjh.zhujianju.enums.BusinessEnum;
import com.mjh.zhujianju.model.base.CommentEO;
import com.mjh.zhujianju.service.base.CommentService;
import com.mjh.zhujianju.util.ApiConstant;
import com.mjh.zhujianju.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author liuyang
 * @Description: 评论操作
 * @date 2018/8/14 0:57
 */
@RestController
@RequestMapping("/comment")
@Api("评论操作")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Resource
    private SysUserService userService;

    @ApiOperation(value="评论", notes="评论操作，返回：{result:ok ->成功，parameter->参数异常}")
    @ApiImplicitParam(name="评论实体类", value="CommentEO", required = true, dataType = "CommentEO")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResponseEntity<JsonResult> add (CommentEO commentEO){
        JsonResult result = new JsonResult();
        try {
            Long userId = XTCurrentUser.getCurrentUserId();
            if(userId != null){
                if(commentEO.getToUserId() != null && commentEO.getToUserId().intValue() == 0){
                    commentEO.setToUserId(null);
                }
                commentEO.setUserId(userId);
                commentEO.setType(BusinessEnum.TYPE_WORKDYNAMIC.getCode());
                String res = commentService.addComment(commentEO);
                result.setStatus(res);
            }else {
                result.setResult("请先登录");
                result.setStatus(ApiConstant.MSG_NO_USER);
            }
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

}
